package net.display;

import javax.servlet.http.HttpServletRequest;

import net.bo.KAffectation;
import net.bo.KPlan;
import net.bo.KRubrique;
import net.ko.displays.KObjectDisplay;
import net.ko.http.views.KPageList;
import net.ko.kobject.KListObject;
import net.ko.kobject.KObject;
import net.technics.ClassTechnics;

public class DisplayPlansActifs extends KObjectDisplay {

	@Override
	public void beforeLoading(Class<? extends KObject> clazz, KPageList list,
			HttpServletRequest request) {
		list.addWhere("active=true");
		super.beforeLoading(clazz, list, request);
	}

	@Override
	public String showInList(KObject ko, String memberName, HttpServletRequest request) {
		String result = super.showInList(ko, memberName, request);

		KPlan plan = (KPlan) ko;
		KAffectation anAffectation = new KAffectation();

		Integer idUtilisateur = (Integer) ClassTechnics.getActiveUser(request).getId();
		anAffectation.setIdUtilisateur(idUtilisateur);
		anAffectation.setIdPlan((Integer) plan.getId());

		if (!arraySearchObject(anAffectation, plan.getAffectations())) {
			if ("libelle".equals(memberName)) {
				result = "<a style='color:#D05754;font-weight:bolder;' id=" + plan.getId() + " class='display'>" + plan.getLibelle() + "</a>";
			}
			if (" ".equals(memberName)) {
				result = "<td><div id='boxButtons'><input id=" + plan.getId() + " class='inscription' type='button' value='Inscription à ce test'></div></td>";
			}
			if ("langage".equals(memberName)) {
				result = "<a style='color:#D05754;font-weight:bolder;' id=" + plan.getId() + " class='display'>" + plan.getLangage() + "</a>";
			}
			if ("Nombre de tests".equals(memberName)) {
				Integer nb = 0;
				KListObject<KRubrique> listeRubriques = plan.getRubriques();
				for (KRubrique kRubrique : listeRubriques) {
					nb = nb + kRubrique.getFonctions().count();
				}
				result = "<a style='color:#D05754;font-weight:bolder;' id=" + plan.getId() + " class='display'>" + String.valueOf(nb) + "</a>";
			}
		} else {
			if (" ".equals(memberName)) {
				result = "<td><div id='boxButtons'>Inscrit</div></td>";
			}
			if ("libelle".equals(memberName)) {
				result = "<a id=" + plan.getId() + " class='display'>" + plan.getLibelle() + "</a>";
			}
			if ("langage".equals(memberName)) {
				result = "<a id=" + plan.getId() + " class='display'>" + plan.getLangage() + "</a>";
			}
			if ("Nombre de tests".equals(memberName)) {
				Integer nb = 0;
				KListObject<KRubrique> listeRubriques = plan.getRubriques();
				for (KRubrique kRubrique : listeRubriques) {
					nb = nb + kRubrique.getFonctions().count();
				}
				result = "<a id=" + plan.getId() + " class='display'>" + String.valueOf(nb) + "</a>";
			}
		}
		return result;
	}

	public boolean arraySearchObject(KAffectation anAffectation, KListObject<KAffectation> listAffectations) {
		for (KAffectation kAffectation : listAffectations) {
			if ((kAffectation.getIdPlan() == anAffectation.getIdPlan()) && (kAffectation.getIdUtilisateur() == anAffectation.getIdUtilisateur())) {
				return true;
			}
		}
		return false;
	}

}
